######################################
# 生成可执行文件的makefile
######################################
#source file
#源文件，自动找所有.c和.cpp文件，并将目标定义为同名.o文件
SOURCE  := $(wildcard *.c */*.c) $(wildcard *.cpp */*.cpp)
OBJS    := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))
NAME    :=$(wildcard *.c *.cpp)

#项目路径
INC_DIR = inc
LIB_DIR = lib
TAR_DIR = build

#target you can change test to what you want
#目标文件名，输入任意你想要的执行文件名
TARGET  := demo1
# TARGET=$(patsubst %.c, %, $(NAME))


#版本信息
VERSION_STRING := "1.0.1"
DATE_STRING := `date "+20%y.%m.%d %k:%M:%S"`
DESC := "名字描述"
EXT := " rosen" #扩展字段一般填入作者等其他不需要显示的信息如作者
RM = rm -f

#compile and lib parameter
#编译参数
CC := gcc
CXX = g++
#库 .a表示自己编译的库 -l后面的表示使用的其他库
# LDFLAGS = $(LIB_DIR)/xxx.a 
LIBS    := -L. -L$(LIB_DIR)
# LIBS =-lm -lpthread
LDFLAGS := -melf_x86_64 -r --whole-archive -lstdc++
DEFINES :=
INCLUDE := -I. -I$(INC_DIR) -I$(LIB_DIR)
CFLAGS  := -g -W -Wall -Werror -Wextra -pipe -O2 -Os $(DEFINES) $(INCLUDE)
# -std=c2x -fPIE -fpic
CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H -std=c++11 -Wpointer-arith -Wno-unused-parameter
CSTRIP = strip
#链接参数
LINK := $(CXX)

# zig参数
ZCC :=zig cc -target x86_64-linux-gnu
ZFLAGS :=-Drelease-fast


.PHONY: everything objs clean veryclean rebuild

everything : $(TARGET)
  
all : $(TARGET)
  
objs : $(OBJS)
  
rebuild: veryclean everything
                
clean :
    # rm -fr *.so
    # rm -fr *.o
    # rm -fr $(TARGET) $(OBJS)
    -$(RM) $(TARGET) $(OBJS) auto_version.h

strip:
    -$(CSTRIP) $(CENTER_DIR)/$(TARGET)
veryclean : clean
    rm -fr $(TARGET)

# main:
# 	@echo build main ...
# 	@echo SOURCE=$(SOURCE)
# 	@echo OBJS=$(OBJS)
# 	@echo NAME=$(NAME)
# 	@echo TARGET=$(TARGET)
# 	@echo done ...

$(TAR_DIR)/$(TARGET):auto_version.h $(OBJS)
    $(CXX) -o $@ $^ $(INCLUDE) $(CXXFLAGS) $(LDFLAGS) $(LIBS)
$(OBJS):%.o:%.c
    $(CXX) -c $(INCLUDE) $(LIBS) $(CXXFLAGS) $< -o $@

#用来生成头文件 然后需要获取版本信息时直接获取宏定义对应的字符串就好了
auto_version.h:
	`touch auto_version.h`
	@echo "#define VER_AUTO 1" > auto_version.h                         	# > :覆盖文本原来内容
	@echo "#define V_NAME \"$(TARGET)\"" >> auto_version.h              	# >> :追加内容到文本末尾
	@echo "#define V_VERSION \"$(VERSION_STRING)\"" >> auto_version.h   	# >> :追加内容到文本末尾
	@echo "#define V_DATE \"$(DATE_STRING)\""	>> auto_version.h       	# >> :追加内容到文本末尾
	@echo "#define V_EXT \"$(EXT)\""	>> auto_version.h               	# >> :追加内容到文本末尾
	@echo "#define V_DESC \"$(DESC)\""	>> auto_version.h               	# >> :追加内容到文本末尾




$(TARGET): $(OBJS)
	@echo build target ...
	@echo TARGET=$(TARGET)
	@echo OBJS=$(OBJS)
    # @echo $(LINK) $(LDFLAGS) $(INCLUDE) -o $@ $<
	$(LINK) $(LDFLAGS) $(INCLUDE) -o $@  $< 
#   -l bcm2835
	@echo $(LINK) $(LDFLAGS) $(INCLUDE) $^ $< $@ 

strip:
	-$(CSTRIP) $(CENTER_DIR)/$(TARGET)
# demo1: $(OBJS)
# 	@echo link...
# 	$(LINK) -o $@ $^ -l bcm2835
# 	@echo done... 

%.o: %.c
	@echo $(CC) $(CFLAGS) $(LIBS) -o $@ -c $<
	$(CC) $(CFLAGS) $(LIBS) -o $@ -c $<

# libadd.a:
# 	zig build
# 	touch libadd.a

# libi32math.a: libadd.a mul.o
# 	$(LINK) $(LDFLAGS) -o $@ $^

# main: main.o libi32math.a
# 	$(CC) -o main $(CFLAGS) $(LIBS) $< -li32math



